package com.nari.ncps.screen.service.impl.electric;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nari.ncps.screen.entity.ConfigDatasourceInfo;
import com.nari.ncps.screen.entity.electric.ElectricOverviewItem;
import com.nari.ncps.screen.entity.electric.ElectricProdoperPowerGeneral;
import com.nari.ncps.screen.entity.electric.ElectricRealTimeData;
import com.nari.ncps.screen.mapper.electric.ElectricOverviewItemMapper;
import com.nari.ncps.screen.mapper.electric.ElectricRealTimeDataMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nari.ncps.screen.mapper.ne.NeWindResourceMapper;
import com.nari.ncps.screen.service.electric.ElectricRealTimeDataService;
import com.nari.ncps.screen.vo.electric.PlantOperationMonitorVO;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 孙书南
 * @since 2025-11-05
 */
@Service
public class ElectricRealTimeDataServiceImpl implements ElectricRealTimeDataService {
    @Autowired
    private ElectricRealTimeDataMapper electricRealTimeDataMapper;
    @Autowired
    private ElectricOverviewItemMapper electricOverviewItemMapper;

    @Override
    public PlantOperationMonitorVO getPlantOperationMonitor(String orgCode) {
        if(orgCode!=null){
            orgCode="10000166";
        }
        PlantOperationMonitorVO vo=new PlantOperationMonitorVO();
        LambdaQueryWrapper<ElectricRealTimeData> yearQuery = new LambdaQueryWrapper<>();
        yearQuery.eq(ElectricRealTimeData::getOrgCode, orgCode)
                .eq(ElectricRealTimeData::getDateType, 0);  // 年数据
        yearQuery.orderByDesc(ElectricRealTimeData::getStatisticTime);
        yearQuery.last("LIMIT 1");
        ElectricRealTimeData one = electricRealTimeDataMapper.selectOne(yearQuery);
        //查询日装机容量，利用小时数
        LambdaQueryWrapper<ElectricOverviewItem> query = new LambdaQueryWrapper<>();
        query.eq(ElectricOverviewItem::getOrgCode, orgCode)
                .eq(ElectricOverviewItem::getDateType, 0);  // 年数据
        query.orderByDesc(ElectricOverviewItem::getStatisticTime);
        query.last("LIMIT 1");
        ElectricOverviewItem two = electricOverviewItemMapper.selectOne(query);


        if(one!=null){
            vo.setPlantCode(one.getOrgCode());
            vo.setPlantName(one.getOrgName());
            vo.setOperatingCapacity(one.getOperatingCapacity());
            vo.setOutageCapacity(one.getShutdownCapacity());
            vo.setRunningUnits(one.getNumberOfOperatingUnits().intValueExact());
            vo.setOutageUnits(one.getNumberOfShutdowns().intValueExact());
        }
        if(two!=null){
            vo.setInstalledCapacity(two.getTotalInstalledCapacity());
            vo.setUtilizationHours(two.getTotalUtilizationHours());
        }
        return vo;
    }

    @Override
    public boolean saveBatch(Collection<ElectricRealTimeData> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<ElectricRealTimeData> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean updateBatchById(Collection<ElectricRealTimeData> entityList, int batchSize) {
        return false;
    }

    @Override
    public boolean saveOrUpdate(ElectricRealTimeData entity) {
        return false;
    }

    @Override
    public ElectricRealTimeData getOne(Wrapper<ElectricRealTimeData> queryWrapper, boolean throwEx) {
        return null;
    }

    @Override
    public Optional<ElectricRealTimeData> getOneOpt(Wrapper<ElectricRealTimeData> queryWrapper, boolean throwEx) {
        return Optional.empty();
    }

    @Override
    public Map<String, Object> getMap(Wrapper<ElectricRealTimeData> queryWrapper) {
        return null;
    }

    @Override
    public <V> V getObj(Wrapper<ElectricRealTimeData> queryWrapper, Function<? super Object, V> mapper) {
        return null;
    }

    @Override
    public BaseMapper<ElectricRealTimeData> getBaseMapper() {
        return null;
    }

    @Override
    public Class<ElectricRealTimeData> getEntityClass() {
        return null;
    }
}
